package com.kitchen
{
	import com.kitchen.http.Proxy;
	import com.kitchen.http.ShopProxy;
	import com.kitchen.http.ShoperProxy;
	import com.kitchen.model.Vo.LoadVo;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	public class SequenceLoaders extends EventDispatcher
	{
		public static const LOAD_F:String = 'loadfinish';
		
		private var app:Main;
		private var index:int;
		private var loads:Array;
		private var loader:Loader;
		private var bg:String = 'bg';
		private var db:String = 'db';
		private var dg:String = 'dg';
		private var si:String = 'si';
		private var pi:String = 'pi';
		
		public function SequenceLoaders(app:Main)
		{
			this.app = app;
			this.loads = new Array();
			this.loads.push(new LoadVo(this.bg, '场景图标素材', '/static/images/main.png', 'BitmapData'));
			this.loads.push(new LoadVo(this.db, '窗口图标素材', '/static/images/dialog.png', 'BitmapData'));
			this.loads.push(new LoadVo(this.dg, '系统模块', 'MasterSprite.swf', 'lib'));
			this.loads.push(new LoadVo(this.si, '店主信息', '', 'proxy'));
			this.loads.push(new LoadVo(this.pi, '店铺信息', '', 'proxy'));
			this.startLoad();
		}
		
		private function startLoad():void{
			this.index = 0;
			this.loader = new Loader();
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandle);
			this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, processHandle);
			this.loopLoad();
		}
		
		private function loopLoad():void{
			var lvo:LoadVo = LoadVo(this.loads[this.index]);
			if(lvo.type == 'lib'){
				var context:LoaderContext = new LoaderContext();
				context.applicationDomain = ApplicationDomain.currentDomain;
				this.loader.load(new URLRequest(lvo.url), context);
			}else if(lvo.type == 'BitmapData'){
				this.loader.load(new URLRequest(lvo.url));
			}else if(lvo.type == 'proxy'){
				if(lvo.name == this.si){
					var shoper:ShoperProxy = new ShoperProxy();
					shoper.addEventListener(Proxy.E_OK, completeHandle);
					shoper.addEventListener(Proxy.E_CERROR, errorHandle);
					shoper.getShoper(this.app.token);
				}else if(lvo.name === this.pi){
					var shop:ShopProxy = new ShopProxy();
					shop.addEventListener(Proxy.E_OK, completeHandle);
					shop.addEventListener(Proxy.E_CERROR, errorHandle);
					if(this.app.shopId == 0){
						this.app.shopId = this.app.shops[0].id;
					}
					shop.getShop(this.app.shoper.id, this.app.shopId);
				}
			}
		}
		
		private function processHandle(pe:ProgressEvent):void{
			var rate:Number = pe.bytesLoaded/pe.bytesTotal;
			this.app.loading.setProcess(rate, this.loads[this.index].title);
		}
		
		private function completeHandle(e:Event):void{
			var vo:LoadVo = LoadVo(this.loads[this.index]);
			if(vo.type == 'BitmapData'){
				if(vo.name == this.bg){
					this.app.mainSkin = Bitmap(this.loader.content).bitmapData;
				}else if(vo.name == this.db){
					this.app.dialogSkin = Bitmap(this.loader.content).bitmapData;
				}
				this.loader.unload();
			}else if(vo.type == 'lib'){
				if(vo.name == this.dg){
					this.app.master = AbstractModule(this.loader.content);
				}
				this.loader.unload();
			}else if(vo.type == 'proxy'){
				var proxy:Proxy = Proxy(e.target);
				if(vo.name == this.si){
					this.app.shoper = proxy.result.result.shoper;
					this.app.shops = proxy.result.result.shops;
				}else if(vo.name == this.pi){
					this.app.shop = proxy.result.result;
				}
				proxy.removeEventListener(Proxy.E_OK, completeHandle);
				proxy.removeEventListener(Proxy.E_CERROR, errorHandle);
				proxy = null;
			}
			this.index++;
			if(this.index >= this.loads.length){
				this.dispatchEvent(new Event(LOAD_F));
			}else{
				this.app.loading.setProcess(0, '');
				this.loopLoad();
			}
		}
		
		private function errorHandle(e:Event):void{
			this.app.loading.setProcess(0.1, e.toString());
		}
	}
}